package com.example.myapplication.presenter.adapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.myapplication.R;
import com.example.myapplication.bean.GoodsBean;
import com.example.myapplication.view.RefactorView.MyViewPager;

import java.util.List;

public class HotListAdapter<T> extends BaseAdapter {
    private List<T> list;
    private Context context;

    public HotListAdapter(List<T> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_swiper_layout, null);
            MyViewPager vp = convertView.findViewById(R.id.vp);
            TextView title = convertView.findViewById(R.id.loop_dec);
            vp.setAdapter(new WiperListAdapter((List<Integer>) list.get(position), context));
            title.setText("轮播图");
            LinearLayout pctr = convertView.findViewById(R.id.point_ctr);
            for (int i = 0; i < ((List<Integer>) list.get(position)).size(); i++) {
                ImageView ivp = new ImageView(context);
                ivp.setImageResource(R.drawable.white_point);
                pctr.addView(ivp);
            }
            vp.setPagerDuration(vp);
        } else {
            if (position == 1) {
                convertView = null;
            }
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.hotlist_adapter_layout, null);
                holder = new ViewHolder();
                holder.img = convertView.findViewById(R.id.hot_img);
                holder.name = convertView.findViewById(R.id.name);
                holder.price = convertView.findViewById(R.id.price);
                holder.sales = convertView.findViewById(R.id.sales);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.img.setImageResource(((GoodsBean) list.get(position)).getUrl());
            holder.name.setText(((GoodsBean) list.get(position)).getName());
            holder.price.setText("￥" + ((GoodsBean) list.get(position)).getPrice());
            holder.sales.setText("销量：" + ((GoodsBean) list.get(position)).getSales());
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView img;
        TextView name;
        TextView price;
        TextView sales;
    }
}
